#include <bits/stdc++.h>
#define int long long
using namespace std;

int p, q, a, b, n, mod;

class Matrix {
public:
    int n, m, a[101][101];
    Matrix(int N, int M)
    {
        n = N, m = M;
        memset(a, 0, sizeof a);
    }
    Matrix operator*(Matrix& b)
    {
        Matrix c(n, b.m);
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= b.m; j++)
                for (int k = 1; k <= m; k++)
                    c.a[i][j] += a[i][k] * b.a[k][j], c.a[i][j] %= mod;
        return c;
    }
};

Matrix quick_pow(Matrix a, int k)
{
    Matrix ans(a.n, a.n);
    for (int i = 1; i <= a.n; i++)
        ans.a[i][i] = 1;
    while (k) {
        if (k & 1)
            ans = ans * a;
        a = a * a;
        k >>= 1;
    }
    return ans;
}

signed main()
{
    cin >> p >> q >> a >> b >> n >> mod;
    if (n <= 2) {
        cout << (n == 1 ? a : b);
        return 0;
    }
    Matrix A(2, 2);
    A.a[1][1] = p, A.a[1][2] = q, A.a[2][1] = 1;
    Matrix B(2, 1);
    B.a[1][1] = b, B.a[2][1] = a;
    Matrix ANS = quick_pow(A, n - 2) * B;

    cout << ANS.a[1][1];
    return 0;
}